﻿using Role_el.Models;
using Role_el.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace itemApi.Controllers
{
    public class HomeController : ApiController
    {
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="use"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult GetMenuList([FromBody] Users use)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                //获取登录用户Id
                Users userId = db.Users.SingleOrDefault(p => p.Id == use.Id);
                //根据用户id获取对应用户角色Id
                var rolesId_list = db.UserRoles.Where(p => p.UserId == userId.Id).ToList();
                var list1 = rolesId_list.Select(p => new
                {
                    RoleId = p.Roled
                }).ToList();
                //用角色id去获得对应模块id
                List<Modules> data1 = new List<Modules>();
                List<Modules> data2 = new List<Modules>();
                foreach (var item in list1)
                {
                    var moduleId_list = db.RoleModules.Where(p => p.RoleId == item.RoleId).ToList();
                    var list2 = moduleId_list.AsEnumerable();

                    foreach (var itema in list2)
                    {
                        var list3 = db.Modules.FirstOrDefault(p => p.Id == itema.ModuleId);
                        if (list3.ParentId == 0)
                        {
                            data1.Add(list3);
                        }
                        else
                        {
                            data2.Add(list3);
                        }
                    }
                    //获取父级模块
                }

                var list5 = data1.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight
                }).ToList();
                var list6 = data2.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight
                }).ToList();
                return Json(new { code = 0, data1 = list5, data2 = list6, msg = "请求成功！" });
            }

        }
        /// <summary>
        /// 获取登录信息-welcome页面
        /// </summary>
        /// <param name="use"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult GetUsList([FromBody] Users use)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var molist = db.Users.Where(p => p.Id == use.Id);
                var mo = molist.Select(p => new
                {
                    Id=p.Id,
                    LoginName=p.LoginName,
                    IsLockout= p.IsLockout,
                    CreateTime=p.CreateTime,
                    LastLoginTime= p.LastLoginTime,
                    ProtectEmail= p.ProtectEmail,
                    ProtectMTel=p.ProtectMTel
                });
                return Json(new { data = mo.ToList() });
            }
        }
        /// <summary>
        /// 修改密码-welcome页面
        /// </summary>
        /// <param name="pa"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult UpdatePass([FromBody] Pass pa)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var user = db.Users.FirstOrDefault(p => p.Id == pa.Id);
                if (user != null)
                {
                    if (user.Password == pa.password)
                    {
                        user.Password = pa.newpassword;
                        int n = db.SaveChanges();
                        if (n > 0)
                        {
                            return Json(new { code = 0, msg = "密码修改成功！" });
                        }
                        else
                        {
                            return Json(new { code = -1, msg = "密码修改失败！" });
                        }
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "原始密码输入不正确！" });
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "用户不存在！" });
                }
            }
        }
    }
}